/**
 * html:
 * <div class="orderCenDiv">
 <div class="tabDiv">
 <table class="userTab certiTab" id="articleGrid" width="100%" border="0" cellspacing="0" cellpadding="0">
 <tr>
 <th>编号</th>
 <th>显示位置</th>
 <th>文章类型</th>
 <th>标题</th>
 <th>状态</th>
 <th>访问量</th>
 <th>详情</th>
 </tr>
 </table>
 </div>
 <!--分页-->
 <div class="page" id="pager"></div>
 </div>
 javascript:
 $("#pager").createPage({
    url: _pageUrl,
    ajaxBackFn: function (data) {
        var _gridHtml = '';
        $.each(data.rows, function (index, row) {
            _gridHtml += '<tr>' +
                '<td><input type="hidden" class="rowid" value=\"' + row.id + '\"><p>' + row.id + '</p></td>' +
                '<td><p>' + row.positionName + '</p></td>' +
                '<td><p>' + row.articleTypeName + '</p></td>' +
                '<td><p>' + row.name + '</p></td>' +
                '<td><p>' + row.statusName + '</p></td>' +
                '<td><p>' + row.viewCount + '</p></td>' +
                '<td><span class="userSpan"><p>' +
                '<a href="javascript:editData(' + row.id + ')">编辑</a>' +
                '<a class="ico" href="javascript:detailData(' + row.id + ')">详情</a>' +
                '</p></span></td></tr>';
        });
        $("#articleGrid tr:not(:first)").html("");
        $('#articleGrid').append(_gridHtml);
    }
});
 **/
;(function ($) {
    $.fn.createPage = function (options) {
        var defaults = {
            pageTotal: 0,//总页数
            pageCur: 1,//当前页
            pageSize: 10,//每页显示条数
            pageCount: 0,//总条数
            url: '',//表格请求路径
            params: {},//请求参数
            search: function (params) {
            },//查询函数
            ajaxBackFn: function (data) {
            }//表格数据请求后的回调函数
        };
        var settings = $.extend(defaults, options);
        var $this = this;
        var $$this = $(this);
        var _initPage = function () {
            return (function () {
                $$this.empty();
                var _p = "";
                _p += '<span><a href="javascript:void(0)" class="first"><em>&lt;&lt;</em></a>' +
                    '<a href="javascript:void(0)" class="previous"><em>&lt;</em></a>';
                for (var m = 0; m < settings.pageTotal; m++) {
                    if (settings.pageCur == (m + 1)) {
                        _p += '<a class="cur pageNum" href="javascript:void(0);">' + (m + 1) + '</a>';
                    } else {
                        _p += '<a class="pageNum" href="javascript:void(0);">' + (m + 1) + '</a>';
                    }
                }
                _p += '<a href="javascript:void(0)" class="netx"><em>&gt;</em></a>' +
                    '<a href="javascript:void(0)" class="last"><em>&gt;&gt;</em></a></span>' +
                    '共<span class="total"></span>条<span class="curPage"></span>/<span class="totalPage"></span>页' +
                    '&nbsp;&nbsp;<input class="pageTxt" type="text" value=""/>' +
                    '<input class="pageBut" type="button" value="GO"/>';
                $$this.html(_p);
                if (settings.pageCur > settings.pageTotal) settings.pageCur = settings.pageTotal;
                if (settings.pageCur < 1) settings.pageCur = 1;
                $('.total').html(settings.pageTotal);
                $('.curPage').html(settings.pageCur);
                $('.totalPage').html(settings.pageTotal);
            })();
        };
        var _dynamicPager = function () {
            $("#pager").empty();
            var _p = "";
            _p += '<a href="javascript:void(0)" class="previous"><em>&lt;&lt;</em></a>';
            if(settings.pageCur != 1 && settings.pageCur >= 4 && settings.pageTotal != 4){
                _p +='<a href="javascript:void(0);" class="pageNo">'+1+'</a>';
            }
            if(settings.pageCur-2 > 2 && settings.pageCur <= settings.pageTotal && settings.pageTotal > 5){
                _p +='<span>...</span>';
            }
            var start = settings.pageCur -2,end = settings.pageCur+2;
            if((start > 1 && settings.pageCur < 4)||settings.pageCur == 1){
                end++;
            }
            if(settings.pageCur > settings.pageTotal-4 && settings.pageCur >= settings.pageTotal){
                start--;
            }
            for (;start <= end; start++) {
                if(start <= settings.pageTotal && start >= 1){
                    if(start != settings.pageCur){
                        _p +='<a href="javascript:void(0);" class="pageNo">'+ start +'</a>';
                    }else{
                        _p +='<a class="cur pageNo">'+ start +'</a>';
                    }
                }
            }
            if(settings.pageCur + 2 < settings.pageTotal - 1 && settings.pageCur >= 1 && settings.pageTotal > 5){
                _p +='<span>...</span>';
            }
            if(settings.pageCur != settings.pageTotal && settings.pageCur < settings.pageTotal -2  && settings.pageTotal != 4){
                _p +='<a href="javascript:;" class="pageNo">'+settings.pageTotal+'</a>';
            }
            _p += '<a href="javascript:void(0)" class="netx"><em>&gt;&gt;</em></a>' +
                '共<span class="total"></span>条/<span class="totalPage"></span>页' +
                '<input class="pageTxt" type="text" value=""/>&nbsp;<input class="pageBut" type="button" value="GO"/>';
            $("#pager").append(_p);
            _getPager();
            _addBindClick();
        }
        var _getPager = function () {
            if (settings.pageCur > settings.pageTotal) settings.pageCur = settings.pageTotal;
            if (settings.pageCur < 1) settings.pageCur = 1;
            $('.total').html(settings.pageCount);
            $('.totalPage').html(settings.pageTotal);
        }
        var _addBindClick = function () {
            return (function () {
                $("#pager a,.pageBut").unbind("click");
                $("#pager a,.pageBut").click(function () {
                    var $_this = $(this);
                    if ($_this.attr("class") == "first") {
                        settings.pageCur = 1;
                    } else if ($_this.attr("class") == "previous") {
                        settings.pageCur = (settings.pageCur - 1) <= 1 ? 1 : settings.pageCur - 1;
                    } else if ($_this.attr("class") == "netx") {
                        settings.pageCur = (settings.pageCur + 1) > settings.pageTotal ? settings.pageTotal : (settings.pageCur + 1);
                    } else if ($_this.attr("class") == "last") {
                        settings.pageCur = settings.pageTotal;
                    } else if ($_this.attr("class") == "pageBut") {
                        if ($(".pageTxt").val() == "") return;
                        var pageTex = (Number)($(".pageTxt").val());
                        if (isNaN(pageTex)) return;
                        settings.pageCur = pageTex >= settings.pageTotal ? settings.pageTotal : pageTex;
                        $(".pageTxt").val("");
                    }
                    if ($_this.attr("class") == "pageNo") {
                        settings.pageCur = (Number)($_this.text());
                        settings.params.pageCur = (Number)($_this.text());
                    }
                    _getBaseParams({});
                    _ajaxData();
                });
            })();
        };
        var _ajaxData = function () {
            $.ajax({
                url: settings.url,
                type: 'POST',
                data: settings.params,
                dataType: "json",
                success: function (data) {
                    settings.pageCount = data.total;
                    settings.pageSize = data.pageSize;
                    settings.pageTotal = data.totalPage;
                    // _dynamicPager();
                    if (typeof(settings.ajaxBackFn) == "function") {
                        settings.ajaxBackFn(data);
                    } else {
                        throw new Error("ajaxBackFn is not a function");
                    }
                    _dynamicPager();
                },
                complete: function () {
                },
                error: function () {
                    // alert("数据加载失败");
                }
            });
        };
        var _getBaseParams = function (params) {
            settings.params = $.extend(settings.params,{
                curPage: settings.pageCur <= 1 ? 1 : settings.pageCur,
                pageSize: settings.pageSize
            }, params);
        };
        this.each(function () {
            _initPage();
            _getBaseParams({});
            _ajaxData();
        });
        var $this = this;
        return {
            search: function (params) {
                $this.each(function () {
                    _getBaseParams(params);
                    _ajaxData();
                });
            }
        };
    }
})(jQuery);